package com.blindBox.front.config.timerConfig;

import com.blindBox.front.Timer.ClearUserStatusJob;
import com.blindBox.front.service.UserService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//Quartz定时任务配置类
@Configuration
public class QuartzConfig
{
    private static String JOB_GROUP_NAME = "CLEAR_LAUCHNUM_JOB";
    private static String TRIGGER_GROUP_NAME = "CLEAR_LAUCHNUM_TRIGGER";
    @Autowired
    private UserService userService;
    //定时任务一: 清空所有用户的状态
    //每晚12点准时清空
    @Bean
    public JobDetail clearUserStatus()
    {
        JobDataMap dataMap = new JobDataMap();
        dataMap.put("userService",userService);
        JobDetail jobDetail = JobBuilder.newJob(ClearUserStatusJob.class)
                .withIdentity("clearUserLauchNumJobDetail",JOB_GROUP_NAME)
                .usingJobData(dataMap)
                .storeDurably() //即使没有Trigger关联时，也不需要删除该JobDetail
                .build();
        return jobDetail;
    }

    /**
     * 定时任务1：
     * 同步用户信息Job（触发器）
     */
    @Bean
    public Trigger clearUserStatusJobTrigger() {
        //每天晚上12点执行一次清除任务
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 0 * * ? *");

        //创建触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(clearUserStatus())//关联上述的JobDetail
                .withIdentity("clearUserLauchNumJobTrigger", TRIGGER_GROUP_NAME)//给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
        return trigger;
    }
}
